//
//  NSNetCache.m
//  erhuo
//
//  Created by Jingru Nie on 14/12/31.
//  Copyright (c) 2014年 集易（北京）科技有限公司. All rights reserved.
//

#import "NSNetCache.h"

@interface NSNetCache()
{
    NSString *dir;
    NSString *file;
    NSString *path;
}
@end

@implementation NSNetCache

- (id)init {
    return [self initWithDirectory:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]  FileName:nil];
}

- (id)initWithDirectory:(NSString *)_dir
               FileName:(NSString *)_file
{
    self = [super init];
    dir = _dir;
    file = _file;
    path =[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    return self;
}

- (void)setDirectory:(NSString *)_dir {
    dir = _dir;
}

- (void)setFileName:(NSString *)_file {
    file = _file;
}

- (NSUInteger)cacheExist {
    NSString *pa = [[path stringByAppendingPathComponent:dir] stringByAppendingPathComponent:file];
    BOOL isDir = FALSE;
    BOOL isFile = [[[NSFileManager alloc] init] fileExistsAtPath:pa isDirectory:&isDir];
    return isFile ? CACHEISFILE : (isDir ? CACHEISDIRECTORY : CACHENOTEXIST);
}

- (BOOL)cacheWithData:(NSData *)data
                error:(NSError **)error
             override:(BOOL)override
{
    if (![dir length])
    {
        dir = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
    }
    if (![file length]) {
        NSDictionary *userinfo = [NSDictionary dictionaryWithObjectsAndKeys:@"please specify cache file name.", NSLocalizedDescriptionKey, nil];
        *error = [[NSError alloc] initWithDomain:@"com.erhuoapp.cache" code:1 userInfo:userinfo];
        return FALSE;
    }
    NSString *pa = [path stringByAppendingPathComponent:dir];
    NSFileManager *fm = [[NSFileManager alloc] init];
    BOOL isDir = FALSE;
    BOOL isFile = [fm fileExistsAtPath:pa isDirectory:&isDir];
    if (!isDir)
    {
        if (![fm createDirectoryAtPath:pa withIntermediateDirectories:YES attributes:nil error:error])
        {
            return FALSE;
        }
    }
    pa = [pa stringByAppendingPathComponent:file];
    if (!override) {
        isFile = [fm fileExistsAtPath:pa isDirectory:&isDir];
        if (isFile && !isDir) {
            NSDictionary *userinfo = [NSDictionary dictionaryWithObjectsAndKeys:@"file already exist.", NSLocalizedDescriptionKey, nil];
            *error = [[NSError alloc] initWithDomain:@"com.erhuoapp.cache" code:1 userInfo:userinfo];
            return FALSE;
        }
    }
    return [fm createFileAtPath:pa contents:data attributes:nil];
}

- (void)deleteCache:(NSError **)error {
    if (![dir length])
    {
        dir = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
    }
    if (![file length]) {
        NSDictionary *userinfo = [NSDictionary dictionaryWithObjectsAndKeys:@"please specify cache file name.", NSLocalizedDescriptionKey, nil];
        *error = [[NSError alloc] initWithDomain:@"com.erhuoapp.cache" code:1 userInfo:userinfo];
        return;
    }
    NSString *pa = [path stringByAppendingPathComponent:dir];
    NSFileManager *fm = [[NSFileManager alloc] init];
    pa = [pa stringByAppendingPathComponent:file];
    [fm removeItemAtPath:pa error:error];
}

- (NSString *)cachePath {
    if ([self cacheExist]) {
        return [[path stringByAppendingPathComponent:dir] stringByAppendingPathComponent:file];
    }
    return nil;
}

@end
